#include "stdafx.h"

SheetController::SheetController()
{
	Document* doc = Application::GetDocument();
	pSheet = doc->GetSheet();
}

void SheetController::ModifyContent(const CELLPOS& Pos, const wstring & Content)
{
	// Save to History List, List will free the memory at least
	WriteCommand* pCmd = new WriteCommand();
	pCmd->Init(Pos, Content, pSheet);
	if (pCmd->Execute())
	{
		Document* pDoc = Application::GetDocument();
		pDoc->AddCmdStep(pCmd);	
	}
	else
	{
		delete pCmd;
	}
}

void SheetController::SetColWidth(COL_NUM iCol, int Width)
{
	ChangeColWidthCmd* pCmd = new ChangeColWidthCmd();
	pCmd->Init(Width, iCol, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::SetRowHeight(ROW_NUM iRow, int Height)
{
	ChangeRowHeightCmd* pCmd = new ChangeRowHeightCmd();
	pCmd->Init(Height, iRow, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}


void SheetController::GetSheetData(LinkedList<CellDataItem>& CellDataList)
{
	CellDataList.Clear();
	RowHeader* pCurrentRow = pSheet->GetRowHead();
	while ( pCurrentRow->pCellBelow != 0)
	{
		pCurrentRow = (RowHeader*)(pCurrentRow->pCellBelow);
		CellNode* pCurrentNode = pCurrentRow;
		while (pCurrentNode->pCellRight != 0)
		{
			pCurrentNode = pCurrentNode->pCellRight;
			/*try
			{*/
				CellDataItem item;
				item.pos.iRow = ((Cell*)pCurrentNode)->GetRowNumber();
				item.pos.iCol = ((Cell*)pCurrentNode)->GetColumnNumber();
				item.pContent = ((Cell*)pCurrentNode)->GetContent();
				item.bkColor = ((Cell*)pCurrentNode)->GetBackColor();
				item.txColor = ((Cell*)pCurrentNode)->GetTextColor();
				item.textHAlign = ((Cell*)pCurrentNode)->GetTextHAlign();
				item.textVAlign = ((Cell*)pCurrentNode)->GetTextVAlign();
				item.fontAttr = ((Cell*)pCurrentNode)->GetFontAttr();
				item.pFontName = ((Cell*)pCurrentNode)->GetFontName();
				CellDataList.Append(item);
			/*}
			catch(...)
			{
				;
			}*/
		}
	}
}

bool SheetController::GetCellContent(const CELLPOS& pos, CellDataItem& item)
{
	Cell* pCell = pSheet->FindCell(pos.iRow, pos.iCol);
	if (pCell!=0)
	{
		item.pos = pos;
		item.pContent = pCell->GetContent();
		item.bkColor = pCell->GetBackColor();
		item.txColor = pCell->GetTextColor();
		item.textHAlign = pCell->GetTextHAlign();
		item.textVAlign = pCell->GetTextVAlign();
		item.fontAttr = pCell->GetFontAttr();
		item.pFontName = pCell->GetFontName();
		return true;
	}
	return false;
}

void SheetController::InsertRow(ROW_NUM iRow)
{
	InsertRowCmd* pCmd = new InsertRowCmd();
	pCmd->Init(iRow, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::InsertCol(COL_NUM iCol)
{
	InsertColCmd* pCmd = new InsertColCmd();
	pCmd->Init(iCol, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::DeleteRegion(const CELLPOS& start, const CELLPOS& end)
{
	DeleteRegionCmd* pCmd = new DeleteRegionCmd();
	pCmd->Init(start, end, pSheet);
	if(pCmd->Execute())
	{
		Document* pDoc = Application::GetDocument();
		pDoc->AddCmdStep(pCmd);
	}
	else
		delete pCmd;
}

void SheetController::CopyRegion(const CELLPOS& start, const CELLPOS& end)
{
	CopyRegionCmd* pCmd = new CopyRegionCmd();
	pCmd->Init(start, end, pSheet);
	pCmd->Execute();
}

void SheetController::CutRegion(const CELLPOS& start, const CELLPOS& end)
{
	CopyRegionCmd* pCmd = new CopyRegionCmd();
	pCmd->Init(start, end, pSheet);
	pCmd->Execute();
	DeleteRegion(start, end);
}

void SheetController::Paste(const CELLPOS& currentPos)
{
	PasteCmd* pCmd = new PasteCmd();
	pCmd->Init(currentPos, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::SetColor(const CELLPOS& start, const CELLPOS& end, const ModifiedField& mf, const COLORREF& col, bool clear)
{
	SetColorCmd* pCmd = new SetColorCmd();
	pCmd->Init(start, end, col, mf, pSheet, clear);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::SetAlign(const CELLPOS& start, const CELLPOS& end, const ModifiedField& mf, UINT aln)
{
	SetAlignCmd* pCmd = new SetAlignCmd();
	pCmd->Init(start, end, aln, mf, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::RemoveRow(ROW_NUM iRow)
{
	RemoveRowCmd* pCmd = new RemoveRowCmd();
	pCmd->Init(iRow, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::RemoveCol(COL_NUM iCol)
{
	RemoveColCmd* pCmd = new RemoveColCmd();
	pCmd->Init(iCol, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::SetFontAttr(const CELLPOS& start, const CELLPOS& end, const ModifiedField& mf, UINT attr)
{
	SetFontAttrCmd* pCmd = new SetFontAttrCmd();
	pCmd->Init(start, end, attr, mf, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}

void SheetController::SetFont(const CELLPOS& start, const CELLPOS& end, const ModifiedField& mf, PTSTR FontName, UINT attr)
{
	SetFontCmd* pCmd = new SetFontCmd();
	pCmd->Init(start, end, attr, FontName, mf, pSheet);
	pCmd->Execute();
	Document* pDoc = Application::GetDocument();
	pDoc->AddCmdStep(pCmd);
}